extends BaseEnemy

# Goblin特定属性
@export var patrol_distance: float = 50.0
@export var patrol_speed: float = 30.0

var patrol_start_position: Vector2
var patrol_target: Vector2
var idle_texture: Texture2D
var run_texture: Texture2D

@onready var weapon: Sprite2D = $Weapon

func _ready():
	# 设置Goblin属性
	max_health = 60
	speed = 60.0
	damage = 15
	detection_range = 100.0
	attack_range = 25.0
	
	# 加载纹理
	idle_texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/enemies/goblin/goblin_idle_spritesheet.png")
	run_texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/enemies/goblin/goblin_run_spritesheet.png")
	
	patrol_start_position = global_position
	patrol_target = patrol_start_position + Vector2(patrol_distance, 0)
	
	super._ready()
	create_animations()
	
	# 开始巡逻
	change_state(State.PATROL)

func create_animations():
	if not animation_player:
		return
	
	# 使用父类的安全函数获取或创建动画库
	var library = get_or_create_animation_library()
	
	# 创建idle动画（只在不存在时创建）
	if not library.has_animation("idle"):
		var idle_animation = Animation.new()
		idle_animation.length = 1.0
		idle_animation.loop_mode = Animation.LOOP_LINEAR
		
		# 添加帧动画轨道
		var track_index = idle_animation.add_track(Animation.TYPE_VALUE)
		idle_animation.track_set_path(track_index, NodePath("Sprite2D:frame"))
		idle_animation.track_insert_key(track_index, 0.0, 0)
		idle_animation.track_insert_key(track_index, 0.17, 1)
		idle_animation.track_insert_key(track_index, 0.33, 2)
		idle_animation.track_insert_key(track_index, 0.5, 3)
		idle_animation.track_insert_key(track_index, 0.67, 4)
		idle_animation.track_insert_key(track_index, 0.83, 5)
		
		library.add_animation("idle", idle_animation)
	
	# 创建run动画（只在不存在时创建）
	if not library.has_animation("run"):
		var run_animation = Animation.new()
		run_animation.length = 0.6
		run_animation.loop_mode = Animation.LOOP_LINEAR
		
		# 添加帧动画轨道
		var run_track = run_animation.add_track(Animation.TYPE_VALUE)
		run_animation.track_set_path(run_track, NodePath("Sprite2D:frame"))
		run_animation.track_insert_key(run_track, 0.0, 0)
		run_animation.track_insert_key(run_track, 0.1, 1)
		run_animation.track_insert_key(run_track, 0.2, 2)
		run_animation.track_insert_key(run_track, 0.3, 3)
		run_animation.track_insert_key(run_track, 0.4, 4)
		run_animation.track_insert_key(run_track, 0.5, 5)
		
		library.add_animation("run", run_animation)

func handle_patrol_state(delta):
	var direction = (patrol_target - global_position).normalized()
	velocity = direction * patrol_speed
	
	# 到达巡逻点后转向
	if global_position.distance_to(patrol_target) < 10:
		if patrol_target.distance_to(patrol_start_position) < 10:
			patrol_target = patrol_start_position + Vector2(patrol_distance, 0)
		else:
			patrol_target = patrol_start_position

func handle_chase_state(delta):
	if player:
		# 更智能的追击 - 预测玩家位置
		var player_velocity = player.velocity if player.has_method("velocity") else Vector2.ZERO
		var predicted_position = player.global_position + player_velocity * 0.3
		var direction = (predicted_position - global_position).normalized()
		
		velocity = direction * speed
		
		var distance_to_player = global_position.distance_to(player.global_position)
		if distance_to_player <= attack_range and attack_timer <= 0:
			change_state(State.ATTACK)

func update_animation():
	if is_dead:
		return
		
	# 更新武器方向
	if weapon:
		if sprite.flip_h:
			weapon.flip_h = true
			weapon.position.x = -abs(weapon.position.x)
		else:
			weapon.flip_h = false
			weapon.position.x = abs(weapon.position.x)
	
	# 更新动画
	match current_state:
		State.IDLE, State.PATROL:
			sprite.texture = idle_texture
			if animation_player.current_animation != "idle":
				safe_play_animation("idle")
		State.CHASE:
			sprite.texture = run_texture
			if animation_player.current_animation != "run":
				safe_play_animation("run")
